package weichat

import (
	"github.com/cliod/wx-go/ma"
)

type Weixin struct {
	AppID     string `json:"AppID"`
	AppSecret string `json:"AppSecret"`
}


//微信初始化 微信的key值和秘钥
func Init(appid string, appsecret string) *Weixin {
	mod := new(Weixin)
	mod.AppSecret = appsecret
	mod.AppID = appid
	return mod
}

//获取微信的openid
func (w *Weixin) GetOpenid(code string) (res interface{},err error){
	conf := ma.NewWxMaConfig(w.AppID, w.AppSecret)
	service := ma.NewWxMaService(conf)
	uc := service.GetWxMaUserService()
	return  uc.GetSessionInfo(code)
}

//微信解密用户敏感信息
func (w *Weixin) GetDecrypt(req *WeiChatReq)(res interface{},err error){
	pc := NewWXUserDataCrypt(w.AppID, req.SessionKey)
	res, err = pc.Decrypt(req.EncryptedData, req.Iv)
	if err != nil {
		return nil,err
	}

	return
}

//获取小程序全局唯一后台接口调用凭据
func (w *Weixin)AccessTokenX()(interface{},error){
	data,err:=AccessToken(w.AppID,w.AppSecret)
	if err!=nil {
		return nil,err
	}
	return data,nil
}

//检查一段文本是否含有违法违规内容
func (w *Weixin) MsgSecCheck()(interface{},error) {
	// 定义一个结构体存储请求参数
	req :=new(CheckStructReq)

	data,err:=MsgSecCheck(w.AppID,w.AppSecret,req)
	if err!=nil {
		return nil, err
	}
	return data, nil
}
